package com.servlet;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.storeconfig.IStoreFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.dao.AnnouncesDao;
import com.model.Announces;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.utils.DBUtil;

/**
 * Servlet implementation class Upservlet
 */
@WebServlet("/Upservlet")
public class Upservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String Path = "C:\\Users\\MDTDS\\Desktop\\forumUpload\\";
	private String title = null;
	private String body = null;
	  private   File file;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Upservlet() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		// 建立session
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		// 设定响应头编码

		if (!FileUpload.isMultipartContent(request)) {
			return;
		}
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(1024 * 1024 * 3);
		factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
		ServletFileUpload serFile = new ServletFileUpload(factory);
		serFile.setFileSizeMax(1024 * 1024 * 10);
		serFile.setSizeMax(1024 * 1024 * 20);

		try {
			List<FileItem> list = serFile.parseRequest(request);
			if (list != null & list.size() > 0) {
				for (FileItem f : list) {
					if (!f.isFormField()) {// 如果不是一般表单参数
						String fileName = new File(f.getName()).getName();
					 file = new File(Path + fileName);
						f.write(file);
						file.getPath();
					} else {
						if ("input-name".equals(f.getFieldName())) {
							title = f.getString("UTF-8");
						} else {
							if ("textarea-name".equals(f.getFieldName())) {
								body = f.getString("UTF-8");
							}
						}
					}
				}
			}
		} catch (FileUploadException e) {

			e.printStackTrace();
		} catch (Exception e) {

			e.printStackTrace();
		}
		
		// 获取前端的数据
		Announces a = new Announces();
		a.setAnnouTitle(title);
		a.setAnnouBody(body);
		a.setFile(file.getPath().replace('\\', '/'));
		AnnouncesDao ann = new AnnouncesDao();
		ann.insert(a);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
